package com.shejiao.goods.controller;

import com.github.pagehelper.PageInfo;
import com.shejiao.goods.pojo.Brand;
import com.shejiao.goods.service.BrandService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value="/brand")
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;
    /***
     * 查询所有品牌
     */
    @GetMapping
    public Result<List<Brand>> findAll(){
        List<Brand> brands = brandService.findAll();


        //响应结果
        return new Result<List<Brand>>(true, StatusCode.OK,"查询所有品牌成功",brands);
    }

    @GetMapping(value="/{id}")
    public  Result<Brand> findById(@PathVariable(value="id") Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK,"查询品牌成功",brand);
    }

    @PostMapping
    public  Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true, StatusCode.OK,"添加商品成功");
    }

    @PutMapping(value="/{id}")
    public Result update(@PathVariable(value="id") Integer id,@RequestBody Brand brand){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK,"更新商品成功");
    }

    @DeleteMapping(value="/{id}")
    public Result delete(@PathVariable(value="id")Integer id){
        brandService.delete(id);
        return new Result(true, StatusCode.OK,"删除商品成功");
    }

    @PostMapping(value="/search")
    public Result<List<Brand>> findList( @RequestBody Brand brand ){
        List<Brand> brands=brandService.findList(brand);
        return new Result<List<Brand>>(true, StatusCode.OK,"搜索品牌成功",brands);
    }

    @GetMapping(value="/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable(value="page")Integer page,
                                    @PathVariable(value="size")Integer size){
        PageInfo<Brand> pageInfo = brandService.findPage(page, size);
        return new Result<PageInfo<Brand>>(true, StatusCode.OK,"分页查询成功",pageInfo);
    }
    @PostMapping(value="/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@RequestBody Brand brand,
                                            @PathVariable(value="page")Integer page,
                                            @PathVariable(value="size")Integer size){
        int q=10/0;
        PageInfo<Brand> pageInfo = brandService.findPage(brand,page, size);
        return new Result<PageInfo<Brand>>(true, StatusCode.OK,"分页查询成功",pageInfo);
    }

}
